<%--
  Created by IntelliJ IDEA.
  User: zhuya
  Date: 2016/1/28
  Time: 16:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="privilege" uri="/WEB-INF/privilege.tld" %>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="format-detection" content="telephone=no">
    <title>忘记密码</title>
    <link rel="stylesheet" href="<c:url value='/resource/wechat/css/frozen.css'/>">
    <link rel="stylesheet" type="text/css" href="<c:url value='/resource/wechat/css/main.css'/>"/>
    <script src="<c:url value='/resource/js/jquery-2.0.min.js'/>"></script>
    <script src="<c:url value='/resource/js/jquery.form.min.js'/>"></script>
    <script src="<c:url value='/resource/wechat/js/zepto.min.js'/>"></script>
    <script src="<c:url value='/resource/wechat/js/frozen.js'/>"></script>
    <script type="text/javascript" src="<c:url value='/resource/wx/js/util.js'/>"></script>
</head>
<body class="body_bg" ontouchstart>
<%--<header class="ui-header ui-header-positive ui-border-b">
  <i class="ui-icon-return" onclick="history.back()"></i>
  <h1>忘记密码</h1>
  <div class="admin_ico"></div>
</header>--%>

<section class="ui-container">
    <div class="ui-form-item ui-form-item-show ui-border-b bg_fff">
        <label for="#">手机号码：</label>
        <input type="text" placeholder="" value="" name="tel" id="loginId" onkeypress="onlyNumber()" maxlength="11">
    </div>


    <div class="ui-form-item ui-form-item-show ui-border-b bg_fff" style="position: relative;">
        <label for="#">图片验证：</label>
        <input type="text" placeholder="" maxlength="4" id="verCode" required value="">
        <img id="codeImg" src="" onclick="getImg()"
             style="position: absolute; top: 4px; right: 10px; width: 75px; height: 36px;">

    </div>

    <div class="ui-form-item ui-form-item-show ui-border-b bg_fff" style="position: relative;">
        <label for="#">验&nbsp;证&nbsp;&nbsp;码：</label>
        <input type="text" placeholder="" name="btnSendCode" id="code" maxlength="6" value="">
        <button class="ui-btn" style="position: absolute; top: 8px; right: 10px;" id="btnSendCode">
            获取验证码
        </button>
    </div>

    <div class="ui-form-item ui-form-item-show ui-border-b bg_fff">
        <label for="#">新&nbsp;密&nbsp;&nbsp;码：</label>
        <input type="password" placeholder="请输入6-20位密码" value="" name="password" id="password" maxlength="20">
    </div>
    <div class="ui-form-item ui-form-item-show ui-border-b bg_fff">
        <label for="#">确认密码：</label>
        <input type="password" placeholder="" id="reappassword" value="" maxlength="20">
    </div>
    <%--  <div class="ui-form-item ui-form-item-show ui-border-b bg_fff">
        <label for="#">身份证号：</label>
        <input type="text" placeholder="" value="" name="idcard" id="idCard" maxlength="18">
      </div>--%>



    <div class="ui-btn-wrap">
        <button class="ui-btn-lg ui-btn-primary" id="Confirmmodif">
            确认更改
        </button>
    </div>
</section>
<script>

    var genGuid;
    $(function () {
        getImg();
    });
    function getImg() {
        genGuid = newGuid().ToString("D");
        $.ajax({
            url: '<c:url value="/api/code/get/'+ genGuid +'" />',
            type: 'get',
            dataType: 'json',
            success: function (resp) {
                if (resp && resp.succ) {
                    $("#codeImg").attr("src", "data:image/png;base64," + resp.data)
                } else {
                    alert(resp.msg);
                }
            }
        });
    }

    $("#Confirmmodif").click(function () {
        submitForm();
    });
    var secs = 300;
    $("#btnSendCode").click(function () {
        var phone = $("#loginId").val();
        if (phone.length != 11) {
            alert("请输入正确的手机号码");
            return;
        }

        var verCode = $("#verCode").val();
        if (!verCode) {
            alert("图片验证码不能为空");
            return;
        }
        getSms(phone, verCode);
/*        $("#btnSendCode").attr("disabled", "true");


        for (var i = 1; i <= secs; i++) {
            window.setTimeout("update(" + i + ")", i * 1000);
        }*/
    });

    function update(num) {
        if (num == secs) {
            $("#btnSendCode").text("发送验证码");
            $("#btnSendCode").removeAttr("disabled");
        }
        else {
            var printnr = secs - num;
            $("#btnSendCode").text(printnr + "秒失效");
        }
    }

    /**
     * 获取验证短信
     * @param phone
     * @param verCode
     */
    function getSms(phone, verCode) {
        $.ajax({
            url: '<c:url value="/api/sms/sendCode" />',
            type: 'post',
            dataType: 'json',
            data: {"tel": phone, "verCode": verCode, "verCodeKey": genGuid, "forget": 1},
            success: function (resp) {
                alert(resp.msg);
                if(resp.succ) {
                    $("#btnSendCode").attr("disabled", "true");
                    for (var i = 1; i <= secs; i++) {
                        window.setTimeout("update(" + i + ")", i * 1000);
                    }
                }
            }
        });
    }

    function onlyNumber() {
        if ((event.keyCode < 48) || (event.keyCode > 57 )) event.returnValue = false;
    }

    function submitForm() {
        var url = "<c:url value='/api/profile/pwd/reset'/>";
        if (checkForm()) {
            $.ajax({
                url: url,
                type: "post",
                data: "idcard=" + $("#idCard").val() + "&tel=" + $("#loginId").val() + "&password=" + $("#password").val() + "&btnSendCode=" + $("#code").val(),
                dataType: "json",
                async: false,
                success: function (resp, status, xhr) {
                    alert(resp.msg);
                    if (resp.succ) {
                        window.location.href = "<c:url value='/weixin/wx_member/login?gowhere=${gowhere}&openId=${openId}'/>";
                    }
                }
            });
        }
    }

    function checkForm() {
        if ($("#loginId").val().length != 11) {
            alert("请输入11位手机号!");
            return false;
        }
        if ($("#code").val() == "") {
            alert("请输入验证码！");
            return false;
        }

        if ($("#password").val().length < 6 || $("#password").val().length > 20) {
            alert("请输入6-20位密码!");
            return false;
        }


        if ($("#reappassword").val() == "" || $("#password").val() == "") {
            alert("请输入确认密码!");
            return false;
        }
        if ($("#reappassword").val() != $("#password").val()) {
            alert("两次输入密码不一致！");
            return false;
        }
        /*if($("#idCard").val().length!=18&&$("#idCard").val().length!=15){
         alert("请输入正确的身份证号码！");
         return false;
         }*/

        return true;
    }

</script>
</body>
</html>
